//从上到下打印出二叉树的每个节点，同一层的节点按照从左到右的顺序打印。 
//
// 
//
// 例如: 
//给定二叉树: [3,9,20,null,null,15,7], 
//
//     3
//   / \
//  9  20
//    /  \
//   15   7
// 
//
// 返回： 
//
// [3,9,20,15,7]
// 
//
// 
//
// 提示： 
//
// 
// 节点总数 <= 1000 
// 
// Related Topics 树 广度优先搜索 
// 👍 33 👎 0

package leetcode.editor.cn;

import common.bean.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * Java：从上到下打印二叉树
 *
 * @author changgui
 */
@SuppressWarnings("all")
public class P剑指_offer_32_i_CongShangDaoXiaDaYinErChaShuLcof {
    public static void main(String[] args) {
        Solution solution = new P剑指_offer_32_i_CongShangDaoXiaDaYinErChaShuLcof().new Solution();
        // TODO 此处开始你的表演

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] levelOrder(TreeNode root) {
            if (root == null) {
                return new int[0];
            }
            List<Integer> list = new ArrayList<>();
            Deque<TreeNode> link = new LinkedList<>();
            link.addFirst(root);
            while (!link.isEmpty()) {
                int size = link.size();
                for (int i = 0; i < size; i++) {
                    TreeNode treeNode = link.removeLast();
                    if (treeNode != null) {
                        list.add(treeNode.val);
                        link.addFirst(treeNode.left);
                        link.addFirst(treeNode.right);
                    }
                }
            }
            int[] nums = new int[list.size()];
            for (int i = 0; i < list.size(); i++) {
                nums[i] = list.get(i);
            }

            return nums;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}